<?php
/**
 * user role
 * @package naanal
 */
class ClsUserrole extends ClsNaanalController
{       
        /**
         * request
         * @package naanal
         * @var string 
         */
	public $request=null;
 
	/**
         * construct
         * @package naanal
         * @param string $controlpanel
         */
	function __construct()
	{
            parent::__construct();
            $this->request=ClsNaanalRequest::getInstance()->getAll();
	}
	/**
         * getRedirect
         * @package naanal
         * @return string
         */
	function getRedirect()
	{
		return $this->redirect;
	}
	/**
         * insert
         * @package naanal
         */
	function insert()
	{       
		$this->mode="insert";
		$obj=new ClsWUserrole();
		$obj->set_user_id($this->request["user_id"]);
                $obj->set_role_id($this->request["role_id"]);
                $obj->update();
      	$this->redirect="{$this->urlModuleParam}=userrole&{$this->urlActionParam}=list";
	}
	/**
         * update
         * @package naanal
         */
	function update()
	{
		$this->mode="update";
		$sql="update `userrole` set role_id=:role_id,user_id=:user_id where `id`=:id";
		$obj=new ClsWUserrole();
                $obj->loadObject($this->request["id"]);
                $obj->set_user_id($this->request["user_id"]);
                $obj->set_role_id($this->request["role_id"]);
                $obj->update();
	    $this->redirect="{$this->urlModuleParam}=userrole&{$this->urlActionParam}=list";
	}
	/**
         * edit
         * @package naanal
         * @return array
         */
	function edit()
	{
		$this->mode="edit";
		$obj=ClsWrapper::getInstance("userrole");
                $obj->loadObject($this->request["id"]);
		return array("form"=>$obj,"lookup"=>$obj->get_user_lookup(), "role_lookup"=>$obj->get_role_lookup());
	}
	/**
         * create
         * @package naanal
         * @return array
         */
	function create()
	{
		$this->mode="create";
		$obj=new ClsWUserrole();
		return array("form"=>$obj, "lookup"=>ClsWrapper::getInstance("userrole")->get_user_lookup(), "role_lookup"=>ClsWrapper::getInstance("userrole")->get_role_lookup());
	}
	/**
         * delete
         * @package naanal
         */
	function delete()
	{
		$this->mode="delete";
		$obj=new ClsWUserrole();
		$obj->set_id($this->request["id"]);
  		$obj->remove();
  		$this->redirect="{$this->urlModuleParam}=userrole&{$this->urlActionParam}=list";
	}
	/**
         * show list
         * @package naanal
         * @return array
         */
	function show_list()
	{
		$this->mode="list";
		$sql="select userrole.* from userrole,user where userrole.user_id=user.id and user.client_id=".getUserObject()->get_client_id();
		$pdo=ClsLPDO::getNamedInstance("userrole");
		$pdo->setQuery($sql);
		$arrObj=$pdo->getAllAssoc();
		//$this->pdo->statement=null;
		//$arrObj=$this->pdo->getAllObject("ClsWUserrole");
		return array("list"=>$arrObj, "lookup"=>ClsWrapper::getInstance("userrole")->get_user_lookup(), "role_lookup"=>ClsWrapper::getInstance("userrole")->get_role_lookup());		
	}
	/**
         * render
         * @package naanal
         * @return string
         */
	function render()
	{     
		$mode=$this->mode;
		if($mode=="list")
			return $this->show_list();
		else
			return $this->$mode();
	}
}
?>